//
//  BrushFaceHomeHeaderView.m
//  BrushFace
//
//  Created by timer_q on 15/12/10.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "BrushFaceHomeHeaderView.h"

@interface BrushFaceHomeHeaderView ()<UIScrollViewDelegate>
{
    NSTimer *timer;
    NSTimer *newTimer;
}
@end

@implementation BrushFaceHomeHeaderView
{
    __weak IBOutlet UIView *topScrollBackView;

    __weak IBOutlet UIView *downScrollBackView;
    UIScrollView *_HScrollView;
    UIScrollView *_VScrollView;
    UIPageControl *_pageControl;
}
- (void)awakeFromNib{
    [self UIConfig];
}
#pragma mark - customMethods
- (void)UIConfig{
    _VScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH/16.0*7)];
    _VScrollView.bounces = NO;
    _VScrollView.pagingEnabled = YES;
    _VScrollView.delegate = self;
    [topScrollBackView addSubview:_VScrollView];
    
    UIImageView *imageView  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _VScrollView.frame.size.width, _VScrollView.frame.size.height)];
    imageView.image = [UIImage imageNamed:@"banner"];
    [_VScrollView addSubview:imageView];
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _VScrollView.frame.size.height-30, topScrollBackView.frame.size.width, 30)];
    _pageControl.numberOfPages = 0;
    [topScrollBackView addSubview:_pageControl];
    
    
    _HScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH/4.0)];
    _HScrollView.bounces = NO;
    _HScrollView.pagingEnabled = YES;
    [downScrollBackView addSubview:_HScrollView];
    

}

- (void)addSubviewToVScrollViewWithViewArray:(NSMutableArray *)viewArray{
    if (viewArray.count==0||viewArray==nil) {
        return;
    }
    for (UIView *view in _VScrollView.subviews) {
        [view removeFromSuperview];
    }
    _pageControl.numberOfPages = viewArray.count;
    _VScrollView.contentSize = CGSizeMake(viewArray.count*_VScrollView.frame.size.width, 0);
    
    for (int i=0; i<viewArray.count; i++) {
        UIView *view = [viewArray objectAtIndex:i];
        view.frame = CGRectMake(i*_VScrollView.frame.size.width, 0, _VScrollView.frame.size.width, _VScrollView.frame.size.height);
        [_VScrollView addSubview:view];
    }
    if (timer) {
        [timer invalidate];
        timer = nil;
    }
    timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(changImage) userInfo:nil repeats:YES];
    [timer fireDate];
}
- (void)addSubViewToHScrollViewWithViewArray:(NSMutableArray *)viewArray{
    if (viewArray.count==0||viewArray==nil) {
        return;
    }
    for (UIView *view in _HScrollView.subviews) {
        [view removeFromSuperview];
    }
    _HScrollView.contentSize = CGSizeMake(viewArray.count*_HScrollView.frame.size.width,0);
    for (int i=0; i<viewArray.count; i++) {
        UIView *view = [viewArray objectAtIndex:i];
        view.frame = CGRectMake(i*_HScrollView.frame.size.width, 0, _HScrollView.frame.size.width, _HScrollView.frame.size.height);
        [_HScrollView addSubview:view];
    }
    if (viewArray.count>=2) {
        newTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changView) userInfo:nil repeats:YES];
        [newTimer fireDate];
    }else{
        [newTimer invalidate];
        newTimer = nil;
    }
}

#pragma mark - scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _pageControl.currentPage = _VScrollView.contentOffset.x/_VScrollView.frame.size.width;
}
- (void)changImage{
    if (_VScrollView.contentOffset.x==SCREEN_WIDTH) {
        [UIView animateWithDuration:0.5 animations:^{
            _VScrollView.contentOffset = CGPointMake(0, 0);
        }];
        
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            _VScrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        }];
    }

}
- (void)changView{
    if (_HScrollView.contentOffset.x==SCREEN_WIDTH) {
        [UIView animateWithDuration:0.5 animations:^{
            _HScrollView.contentOffset = CGPointMake(0, 0);
        }];
        
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            _HScrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        }];
    }
    
}
@end
